package com.shunhe.zs_pda.util

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.annotation.IdRes
import com.ruitu.arad.util.ToastUtils
import com.ruitu.router_module.Logg

// 启动Activity,Bunble传参
inline fun <reified T> openActivity(ctx: Context, bundle: Bundle?) {
    openActivity<T>(ctx) {
        if (bundle != null) {
            putExtras(bundle)
        }
    }
}

// 使用泛型实化和高阶函数实现启动Activity和Intent传参
inline fun <reified T> openActivity(ctx: Context, putPrm: Intent.() -> Unit) {
    val intent = Intent(ctx, T::class.java)
    intent.putPrm()
    ctx.startActivity(intent)
}

fun String.countLetter(): Int {
    var count = 0
    for (c in this) {
        if (c.isLetter()) {
            count++
        }
    }
    return count
}

fun String.toast(duration: Int = Toast.LENGTH_SHORT) {
    if (duration == Toast.LENGTH_LONG) {
        ToastUtils.showLongSafe(this)
    } else {
        ToastUtils.showShortSafe(this)
    }
}

fun log(msg: String) {
    Logg.d(msg)
}

fun logi(msg: String) {
    Logg.i(msg)
}

fun Activity.ui(func: () -> Unit) {
    runOnUiThread { func() }
}

fun <T : View> Activity.find(@IdRes id: Int): T {
    return findViewById<T>(id)
}

fun <T : View> View.find(@IdRes id: Int): T {
    return findViewById<T>(id)
}